﻿//
// ToDoList v1.1
// Coder : Onat Yigit Mercan
// http://onatm.wordpress.com
//
using System;
using System.Windows;
using Microsoft.Win32; // Added for RegistryKey class

namespace ToDoList
{
    /// <summary>
    /// Interaction logic for Configuration.xaml
    /// </summary>
    public partial class Configuration : Window
    {
        private string strKey = @"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
        public Configuration(Window owner)
        {
            this.Owner = owner;
            InitializeComponent();
            CheckRegistry();
        }

        private void CheckRegistry()
        {
            RegistryKey startupKey = Registry.CurrentUser.OpenSubKey(strKey);

            try
            {
                if (startupKey.GetValue("ToDo List") == null)
                {
                    chkStartUp.IsChecked = false;
                }
                else
                {
                    chkStartUp.IsChecked = true;
                }
            }
            catch(Exception)
            {
                System.Windows.Forms.MessageBox.Show("Registry key is broken.", 
                    "ToDo List", System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Error);   
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            RegistryKey startupKey = Registry.CurrentUser.OpenSubKey(strKey, true);

            try
            {
                if (chkStartUp.IsChecked == true)
                {
                    startupKey.SetValue("ToDo List", System.Windows.Forms.Application.ExecutablePath.ToString());
                }
                else if(startupKey.GetValue("ToDo List") == null)
                {
                    // Do nothing
                }
                else
                {
                    startupKey.DeleteValue("ToDo List");
                }
            }
            catch(Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message,
                    "ToDo List", System.Windows.Forms.MessageBoxButtons.OK,
                    System.Windows.Forms.MessageBoxIcon.Error);  
            }
            this.Close();
        }
    }
}
